package com.loby.data.mapper;


import com.loby.data.base.pojo.TMonForwardInfos;
import com.loby.data.base.pojo.TMonMonitorStation;
import com.loby.data.base.pojo.TMonSourceShops;
import com.loby.data.history.pojo.HisDayData;
import com.loby.data.history.pojo.HisHourData;
import com.loby.data.history.pojo.HisMinData;
import com.loby.data.history.pojo.HisRealData;
import com.loby.data.util.TimeDataParam;
import com.loby.data.view.pojo.VStationTree;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MonitorStationMapper {
    /**
     * 新建排放口
     * @param monitorStation
     * @return
     */
    @Insert("INSERT INTO t_mon_monitor_station VALUES (#{pkId},#{stationName},#{stationUniqueCode},#{remotePwd},#{fkPollutionCategoryCode},#{fkStationCategoryCode},#{longitude},#{latitude},#{commProtocol},#{commProtocolIsResponse},#{realInterval},#{minInterval},#{fkWaterDirection},#{closeDate},#{brCode},#{fkShopId})")
    int newMonitorStation(TMonMonitorStation monitorStation);

    @Delete("delete from t_mon_monitor_station where fk_shop_id=#{id}")
    long removeMonitorStationByShopId(@Param("id") long shopId);

    @Delete("delete from t_mon_monitor_station where pk_id=#{id}")
    int removeMonitorStationById(@Param("id")long id);

    @Select({"SELECT * FROM t_mon_forward_infos WHERE fk_station_id=#{id}"})
    List<TMonForwardInfos> getForwardInfosByStationId(@Param("id") long id);

    @Select("select * from t_mon_monitor_station where pk_id=#{id}")
    TMonMonitorStation getMonitorStationById(@Param("id") long id);

    @Select("select * from t_mon_monitor_station where station_unique_code=#{code}")
    TMonMonitorStation getMonitorStationByCode(@Param("code") String code);

    @Select("select * from t_mon_monitor_station where fk_shop_id=#{id}")
    List<TMonMonitorStation> getMonitorStationsByShopId(@Param("id") long shopId);

    @Select("select * from t_mon_source_shops where pk_id=#{id}")
    TMonSourceShops getShopByFkShopId(@Param("id") long fkShopId);



    @Select("select * from v_organization_monitor where org_id=#{id}")
     List<TMonMonitorStation> getMonitorStationsByOrganizationId(@Param("id")long id);


    @Select("SELECT ps_pk_id,fk_shop_id,st_pk_id,org_name,shop_name,station_name,pk_org_code,station_unique_code,shop_no" +
            ",ad_code,fk_pollution_category_code,longitude,fk_station_category_code,latitude FROM v_station_tree" +
            " where find_in_set (st_pk_id,#{keys})")
    List<VStationTree> getMonitorStationTree(String keys);

    @Select("select * from v_station_tree where st_pk_id=#{id}")
    VStationTree getStationByStId(@Param("id") long id);


    @Select("select * from v_station_tree where station_unique_code=#{mnCode}")
    VStationTree getStationByMnCode(@Param("mnCode")String mnCode);


    @Select("SELECT * from (SELECT ps_pk_id,fk_shop_id,st_pk_id,org_name,shop_name,station_name,pk_org_code,station_unique_code,shop_no,ad_code,fk_pollution_category_code,fk_station_category_code,longitude,latitude FROM v_station_tree where find_in_set (st_pk_id,#{keys})) p where  p.ad_code in (SELECT pk_division_code FROM t_code_administrative_division WHERE FIND_IN_SET(pk_division_code,get_child_list(#{code})))")
    List<VStationTree> getChildStationsByAdCode(@Param("code") String adCode,@Param("keys") String keys);

    @Select("SELECT ps_pk_id,fk_shop_id,st_pk_id,org_name,shop_name,station_name,pk_org_code,station_unique_code,shop_no,ad_code,fk_pollution_category_code,fk_station_category_code,longitude,latitude FROM v_station_tree where find_in_set (st_pk_id,#{keys})  and pk_org_code =#{code}")
    List<VStationTree> getStationsByOrgCode(@Param("code") String adCode,@Param("keys") String keys);

    @Select("SELECT *  FROM his_${mnCode}_real_data  ORDER BY data_time desc")
    List<HisRealData> getAllRealDatas(@Param("mnCode") String mnCode);

    @Select("SELECT *  FROM his_${mnCode}_min_data  ORDER BY data_time desc")
    List<HisMinData> getAllMinDatas(@Param("mnCode") String mnCode);

    @Select("SELECT *  FROM his_${mnCode}_hour_data  ORDER BY data_time desc")
    List<HisHourData> getAllHourDatas(@Param("mnCode") String mnCode);

    @Select("SELECT *  FROM his_${mnCode}_day_data  ORDER BY data_time desc")
    List<HisDayData> getAllDayDatas(@Param("mnCode") String mnCode);

    @Select("SELECT *  FROM his_${mnCode}_real_data WHERE data_time>=#{startTime} && data_time <= #{endTime} ORDER BY data_time desc")
    List<HisRealData> getRealDatas(String mnCode, String startTime, String endTime);

    @Select("SELECT *  FROM his_${mnCode}_min_data WHERE data_time>=#{startTime} && data_time <= #{endTime} ORDER BY data_time desc")
    List<HisMinData> getMinDatas(@Param("mnCode") String mnCode,@Param("startTime") String startTime, @Param("endTime")String endTime);

    @Select("SELECT *  FROM his_${mnCode}_hour_data WHERE data_time>=#{startTime} && data_time <= #{endTime} ORDER BY data_time desc")
    List<HisHourData> getHourDatas(@Param("mnCode") String mnCode,@Param("startTime") String startTime, @Param("endTime")String endTime);

    @Select("SELECT *  FROM his_${mnCode}_day_data WHERE data_time>=#{startTime} && data_time <= #{endTime} ORDER BY data_time desc")
    List<HisDayData> getDayDatas(@Param("mnCode") String mnCode,@Param("startTime") String startTime, @Param("endTime")String endTime);

    @Select("SELECT * from his_${code}_real_data WHERE pk_data_id in (\n" +
            "(SELECT pk_data_id FROM `his_${code}_real_data` WHERE data_time=#{time})-20,\n" +
            "(SELECT pk_data_id FROM `his_${code}_real_data` WHERE data_time=#{time})-40,\n" +
            "(SELECT pk_data_id FROM `his_${code}_real_data` WHERE data_time=#{time})-60,\n" +
            "(SELECT pk_data_id FROM `his_${code}_real_data` WHERE data_time=#{time})-80,\n" +
            "(SELECT pk_data_id FROM `his_${code}_real_data` WHERE data_time=#{time})-100,\n" +
            "(SELECT pk_data_id FROM `his_${code}_real_data` WHERE data_time=#{time})-120\n" +
            "\n" +
            ") ORDER BY data_time ")
    List<HisRealData> getCharDatas(@Param("code")String mnCode,@Param("time")String time);


}
